package com.xiaoai.httpClientTestService.httpclient.javaf;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.net.URIBuilder;

public class HttpclientGetParam {

    public static void main(String[] args) throws Exception {
        //创建httpget对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建URIBuilder
        URIBuilder uriBuilder = new URIBuilder("http://localhost:8080/test/getpGetParam");
        // 多参数可用链式编程
        uriBuilder.setParameter("name","xiaoai")
                .setParameter("sex","nan");

        // 创建get请求
        HttpGet httpGet = new HttpGet(uriBuilder.build());

        CloseableHttpResponse response = null;
        // 发起请求，获取response
        response = httpClient.execute(httpGet);
        // 解析
        if (response.getCode() == 200){
            String content  = EntityUtils.toString(response.getEntity(),"utf8");
            System.out.println(content);
        }
        response.close();
        httpClient.close();
    }
}
